package com.lancoo.examview;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.lancoo.answer.model.ConstantBean;
import com.lancoo.examview.test.PaperActivity;
import com.lancoo.answer.model.TaskState;
import com.stkouyu.xiansheng.SSoundManager;

import java.io.File;

/**
 * Created by pengmi on 2021/12/7 15:04
 * Description:
 */
public class TestActivity extends AppCompatActivity {

    private long subTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        findViewById(R.id.btn1).setOnClickListener(v -> {
            if(System.currentTimeMillis()-subTime<200){
             return;
            }
            subTime=System.currentTimeMillis();
            PaperActivity.launch(TestActivity.this,"TestData.Json",TaskState.TRAINING);
        });
        findViewById(R.id.btn2).setOnClickListener(v -> {
            if(System.currentTimeMillis()-subTime<200){
                return;
            }
            subTime=System.currentTimeMillis();
            PaperActivity.launch(TestActivity.this,"TestData.Json",TaskState.LOOKING);
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent=new Intent(TestActivity.this, ResultActivity.class);
                startActivity(mIntent);
            }
        });
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(System.currentTimeMillis()-subTime<200){
                    return;
                }
                subTime=System.currentTimeMillis();
                PaperActivity.launch(TestActivity.this,"TestData1.Json",TaskState.TRAINING);
            }
        });
        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(System.currentTimeMillis()-subTime<200){
                    return;
                }
                subTime=System.currentTimeMillis();
                PaperActivity.launch(TestActivity.this,"TestData3.Json",TaskState.TRAINING);
            }
        });
        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(System.currentTimeMillis()-subTime<200){
                    return;
                }
                subTime=System.currentTimeMillis();
                PaperActivity.launch(TestActivity.this,"TestData5.json",TaskState.TRAINING);
            }
        });
        findViewById(R.id.btn7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(System.currentTimeMillis()-subTime<200){
                    return;
                }
                subTime=System.currentTimeMillis();
                PaperActivity.launch(TestActivity.this,"TestData6.json",TaskState.LOOKING);
            }
        });
        findViewById(R.id.btn8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(System.currentTimeMillis()-subTime<200){
                    return;
                }
                subTime=System.currentTimeMillis();
                PaperActivity.launch(TestActivity.this,"ResultTestData.Json",TaskState.LOOKING);
            }
        });
        findViewById(R.id.btn10).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(System.currentTimeMillis()-subTime<200){
                    return;
                }
                subTime=System.currentTimeMillis();
                PaperActivity.launch(TestActivity.this,"data.json",TaskState.TRAINING);
            }
        });
        if (Build.VERSION.SDK_INT >= 23) {
            verifyPermissions(this);
        } else {
            initEngine();
        }

    }

    private void verifyPermissions(Activity activity) {
        if (activity == null) {
            return;
        }
        Log.e("eeee", "权限获取");
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
        ) {
//            Toast.makeText(getApplicationContext(), "没有获取权限,请手动开启权限", Toast.LENGTH_SHORT).show();
            // 申请一个（或多个）权限，并提供用于回调返回的获取码（用户定义）
            String[] pressisionArray;
            try {
                pressisionArray = new String[]{Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
                if (pressisionArray.length == 0) {
                    return;
                }
                Log.e("eeee", "权限不足，请求响应");
                ActivityCompat.requestPermissions(activity, pressisionArray, 100);//,Manifest.permission.READ_PHONE_STATE
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            initEngine();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.e("eeee", "权限请求回调onRequestPermissionsResult" + requestCode);
        switch (requestCode) {
            case 100:
                if (grantResults == null || grantResults.length == 0) {
                    return;
                }
                initEngine();
                break;
            case 323:
                break;
        }
    }


    private void initEngine() {
        SSoundManager.getInstance(this);
        String path="/storage/emulated/0/Android/data/com.example.taskdemo/files/record/klg";
        File file=new File(path);
        if(!file.exists()){
            file.mkdir();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SSoundManager.release();
    }



}
